#include "voicechatui.h"
#include "ui_voicechatui.h"
#include "Entity_h/user.h"
#include "Net_h/NetServerConfig.h"
#include "Error_h/error.h"
#include <QDebug>
#include <QMessageBox>
#include <QComboBox>

VoiceChatUI::VoiceChatUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VoiceChatUI)
{
     ui->setupUi(this);
    _curUser = new User();
    _curUser->SetFilePath("E:\\nihao.raw");
    connect(_curUser,SIGNAL(InformUI()),this,SLOT(ShowNewMessage()));

//不能在界面文件以外添加，why
//    ui->comboBoxFriendList->addItem
//    ui->comboBoxFriendList->addItem("王鹏程");

    //当下拉列表框中的项重新选择时 产生一个currentIndexChanged(int index)信号 弹出一个消息框提示你重新选择了哪一项
    connect(ui->comboBoxFriendList, SIGNAL(currentIndexChanged(int)), this, SLOT(onFriendChanged(int)));
}

VoiceChatUI::~VoiceChatUI()
{
    delete ui;
}

void VoiceChatUI::on_pushButtonRecord_clicked()
{
    _curUser->Record();
}

void VoiceChatUI::on_pushButtonPlay_clicked()
{
     _curUser->Play();
}

void VoiceChatUI::on_pushButtonFinish_clicked()
{
     _curUser->Finish();
}

void VoiceChatUI::on_pushButtonConnect_clicked()
{
    //从配置类中读取服务器IP和port
    QString ip = NetServerConfig::GetInstance()->GetServerIp();
    QString port = NetServerConfig::GetInstance()->GetServerPort();

    QString curFriend = ui->comboBoxFriendList->currentText();
    qDebug()<<curFriend;
    ui->labelInformation->setText("正在连接到服务器...");

    _curUser->SetServer(ip,port);
    if (_curUser->ConnectServer()==NONE)  ui->labelInformation->setText("连接成功，等待好友"+curFriend+"回应！");
    else  ui->labelInformation->setText("连接失败，请重试%>_<%...");

}

void VoiceChatUI::onFriendChanged(int index){
    QMessageBox::warning(this, "Message", ui->comboBoxFriendList->itemText(index), QMessageBox::Ok);
}

void VoiceChatUI::SolveMessage(){

}
